Welcome to Application Instance!

1Recursive method to check BOM

1递归法查BOM

import pandas as pd

#样本BOM数据(你可以用实际的DataFrame替换此数据)

# bom_data = {

# '父物料': ['A', 'A', 'B', 'C', 'A1', 'A1', 'A2', 'A11', 'B1'],

'子物料': ['A1', 'A2', 'B1', 'C1', 'A11', 'A12' , 'A12', 'A111', 'B11']


import time

start_time = time.time()

path=r "D:\Pyobject2023\object\测试\bom测试.xlsx"

df=pd.read_excel(path)

# 创建DataFrame

df_bom = pd.DataFrame(df)

# 指定查询的产品(Series数据)

search_products = pd.Series([ "1205-02T-0B-2",'1261-102-36-8' ])


def find_children(df, parent, level):

" 递归查找给定父物料的所有子物料及其层级。

result = []

children = df[df[ '父物料' ] == parent]

print ( children)

for _, row in children.iterrows():

print (row)

child = row[ '子物料' ]

b1=row[ "子件名称" ]

b2 = row[ "数量" ]

b3 = row[ "按系列" ]

b4 = row[ "单位" ]

print (child )

result.append((product,parent, child, level,b1,b2,b3,b4))

result.extend(find_children(df, child, level + 1))

print (result)

return result


# 收集所有指定产品的结果

results = []

for product in search_products:

results.extend(find_children(df_bom, product, 1))

# 创建结果DataFrame

df_results = pd.DataFrame(results, columns=[ "产品",'父物料', '子物料', '层级',"子件名称","数量","按系列","单位" ])

# 显示结果DataFrame

print (df_results)

df_results.to_excel( "BOM整理77777777.xlsx" )

end_time = time.time()

duration = end_time - start_time

print (f""运行时长: {duration}秒"")

"